﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.Data.Services;
using System.Data.Services.Client;

namespace ODatas.Server
{
    public class CalcService : ICalculator
    {
        public long Add(long x, long y)
        {
            return x + y;
        }

        public long Subtract(long x, long y)
        {
            return x - y;
        }

        public long Multiply(long x, long y)
        {
            return x * y;
        }

        public long Divide(long x, long y)
        {
            return x / y;
        }

        public string SayHello(string name)
        {
            return "Hello " + name;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //RestFullService();

            ODataService();
        }

        static void ODataService()
        {
            Uri[] baseAddress = new Uri[] { new Uri("http://localhost:8000/") };
            var service = new OrderItems();
            var host = new DataServiceHost(typeof(OrderItems), baseAddress);
            host.Open();

            DataServiceContext client = new DataServiceContext(baseAddress[0]);
            var q = client.CreateQuery<Order>("Orders");
            //  .AddQueryOption("name", "'2'");
            Console.WriteLine("Querying for " + q.ToString());
            foreach (var c in q)
            {
                Console.WriteLine("Customer: " + c.Customer);
            } 

        }

        static void RestFullService()
        {
            Uri baseAddress = new Uri("http://localhost:8000/");

            ServiceHost svcHost = new ServiceHost(typeof(CalcService), baseAddress);
            WebHttpBinding binding = new WebHttpBinding();
            ServiceEndpoint endpoint = svcHost.AddServiceEndpoint(typeof(ICalculator), binding, baseAddress);
            WebHttpBehavior httpBehavior = new WebHttpBehavior();
            endpoint.Behaviors.Add(httpBehavior);
            try
            {
                svcHost.Open();

                Console.WriteLine("Service is running");
                Console.WriteLine("Press enter to quit...");
                Console.ReadLine();

                svcHost.Close();
            }
            catch (CommunicationException cex)
            {
                Console.WriteLine("An exception occurred: {0}", cex.Message);
                svcHost.Abort();
            }
        }
    }
}
